<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use Myredis\Myredis;
use think\Request;
use think\Route;
use think\Session;
class Details extends Controller
{

    public function _initialize()
    {
        if(Request::instance()->session('uid')){
            $stoken = Request::instance()->session('token');
            $rtoken = MyRedis::get(Request::instance()->session('tel') . 'token');
            if (!empty($rtoken)) {
                if ($stoken != $rtoken) {
                    Session::clear();
                    return $this->success('账号异常登录,请注意账号安全', url('index/index/index'));
                }
            }
        }
    }

    public function newpic()
    {
        $uid = input('session.uid');
        if (empty(input('id'))) {
            return $this->redirect('index/error/index');
        } else {
            $id = input('id');
            $data = Db::table('xo_goods_up')->field(true)->where(['id' => $id])->find();
            if (empty($data)) {
                return $this->redirect('index/error/index');
            }

            $data['size'] = round($data['size'] / 1024, 2);
            $ugid = $data['uid'];
            //查询当前作者的相关信息
            $fanscount = Db::table('xo_user_friends')->where(['fid' => $ugid])->count();
            $goodscount = Db::table('xo_goods_up')->where([
                'uid' => $ugid,
                'check' => 1
            ])->count();
            //关注状态
            $status = Db::table('xo_user_friends')->where(
                ['uid' => Request::instance()->session('uid'),
                    'fid' => $ugid
                ])->find();
            //收藏状态
            $collstatus = Db::table('xo_user_goods')->where(['uid' => Request::instance()->session('uid'), 'gid' => $id])->find();
            $userinfo = [
                'fanscount' => $fanscount,
                'goodscount' => $goodscount,
                'status' => $status,
                'collstatus' => $collstatus
            ];

            $name = Db::table('xo_user')->field('nickname')->where(['id' => $ugid])->find();
            $name = $name['nickname'];
            $sold = Myredis::hget('sold', $id);

            if (empty($sold)) {
                $sold = Db::table('xo_goods_up')->field('sold')->where(['id' => $id])->find();
                $sold = $sold['sold'];
            };

            $icon = Db::table('xo_user')->where(['id' => $ugid])->field('icon')->find();

            $list = Db::table('xo_goods_up')->where(['uid' => $ugid])->field(['icon', 'id'])->order('sold', 'desc')->limit(3)->select();
            $look = Myredis::hincrby('look', $id, '1');

            $dw_num = Db::table('xo_user')->field('dw_num')->where(['id' => $uid])->find();
            $dw_num = $dw_num['dw_num'];

            return view('index/newpic', ['data' => $data, 'icon' => $icon['icon'], 'list' => $list, 'sold' => $sold, 'look' => $look, 'dw_num' => $dw_num, 'name' => $name, 'userinfo' => $userinfo]);
        }
    }

    /**
     * 个人主页
     * @return \think\response\View
     */
    public function home()
    {
        $uid = input('uid');
        if (empty($uid)) {
            $uid = Request::instance()->session('uid');
        }
        $abc = Db::table('xo_user')->field('nickname')->where(['id' => $uid])->find();
        //作者主页
        $name = $abc['nickname'];
        $res = Db::table('xo_user')->field(['icon', 'id'])->where(['id' => $uid])->find();
        if (empty($res)) {
            return $this->redirect('index/error/index');
        }
        //拿取个人的作品数量
        $count = DB::table('xo_goods_up')->where(['uid' => $uid])->count();
        //通过数量进行分页
        $data = Db::table('xo_goods_up')->field(true)->where(['uid' => $uid])->order('addtime', 'desc')->paginate(16, $count);
        $page = $data->render();
        //拿取个人的头像和id
        //拿取该用户有多少粉丝
        $fas = Db::table('xo_user_friends')->where(['fid' => $res['id']])->count();
        //拿取该用户有多少关注
        $fol = Db::table('xo_user_friends')->where(['uid' => $res['id']])->count();
        //获取用户的关注状态
        $status = Myredis::sismber(Request::instance()->session('uid') . '_friends', $res['id']);
        //返回页面
        return view('index/home', ['data' => $data->items(), 'name' => $name, 'page' => $page, 'res' => $res, 'fas' => $fas, 'status' => $status, 'fol' => $fol]);
    }

    /**
     * 获取当前用户关注列表
     * @return \think\response\View|void
     */
    public function attention()
    {
        //获取当前用户的id
        $id = Request::instance()->session('uid');
        //通过当前用户的id获取自己的关注列表
        $res = Db::table('xo_user_friends')->where(['uid' => $id])->field('fid')->paginate(3);
        $page = $res->render();
        //获取当前用户的基本信息
        $userinfo = Db::table('xo_user')->field(['icon', 'nickname'])->find($id);
        //获取当前用户的粉丝数量
        $facecount = Db::table('xo_user_friends')->where(['fid' => $id])->field(['uid'])->count();
        //获取当前关注数量
        $friendscount = Db::table('xo_user_friends')->where(['uid' => $id])->field('fid')->count();

        if ($res->items()) {
            //获取关注用户的基本信息
            foreach ($res as $key => $val) {
                $friends[] = Db::table('xo_user')->field(['nickname', 'icon', 'id'])->find($val);
            }
            foreach ($friends as $key => $val) {
                //获取关注用户的基本信息
                $imgs[] = Db::table('xo_goods_up')->where([
                    'uid' => $val['id'],
                    'check' => 1
                ])->field(['uid', 'icon'])->limit(1)->select();
                //获取关注用户的作品数量
                $goods[] = Db::table('xo_goods_up')->where([
                    'uid' => $val['id'],
                    'check' => 1
                ])->count();

                //获取粉丝的粉丝数量
                $userfans[] = Db::table('xo_user_friends')->where([
                    'fid' => $val['id']
                ])->count();

                foreach ($imgs as $k => $v) {
                    if (empty($v)) {

                    } else {
                        $val['imgs'] = $v[0]['icon'];
                    }
                    foreach ($goods as $kk => $vv) {
                        $val['goodscount'] = $vv;
                    }
                    foreach ($userfans as $fan => $c) {
                        $val['fanscount'] = $c;
                    }

                }
                $friendsinfo[] = $val;
            }
        } else {
            $friendsinfo = 0;
        }
        return view('index/attention', [
            'friendsinfo' => $friendsinfo,
            'facecount' => $facecount,
            'friendscount' => $friendscount,
            'userinfo' => $userinfo,
            'page' => $page
        ]);
    }

    /**
     * 粉丝列表
     * @return \think\response\View
     */
    public function fans()
    {
        $id = Request::instance()->session('uid');
        //获取当前用户的id
        $id = Request::instance()->session('uid');
        //通过当前用户的id获取自己关注的粉丝列表
        $res = Db::table('xo_user_friends')->where(['fid' => $id])->field('uid')->paginate(3);
        $page = $res->render();
        //获取当前用户的基本信息
        $userinfo = Db::table('xo_user')->field(['icon', 'nickname'])->find($id);
        //获取当前用户的粉丝数量
        $facecount = Db::table('xo_user_friends')->where(['fid' => $id])->field(['uid'])->count();
        //获取当前用户的关注数量
        $friendscount = Db::table('xo_user_friends')->where(['uid' => $id])->field('fid')->count();
        if ($res->items()) {
            //通过粉丝列表id获取他们的相关信息
            foreach ($res as $key => $val) {
                $fans[] = Db::table('xo_user')->field(['nickname', 'icon', 'id'])->find($val);
            }
            //通过粉丝相关信息中的name获取他们的作品以及作品数量
            foreach ($fans as $key => $val) {
                //获取粉丝的一个作品
                $imgs[] = Db::table('xo_goods_up')->where([
                    'uid' => $val['id'],
                    'check' => 1
                ])->field(['id', 'icon'])->limit(1)->select();
                //获取粉丝的作品数量
                $goods[] = Db::table('xo_goods_up')->where([
                    'uid' => $val['id'],
                    'check' => 1
                ])->count();
                //获取粉丝的粉丝数量
                $userfans[] = Db::table('xo_user_friends')->where([
                    'fid' => $val['id']
                ])->count();
                foreach ($imgs as $k => $v) {
                    if (empty($v)) {

                    } else {
                        $val['imgs'] = $v[0]['icon'];
                    }
                    foreach ($goods as $kk => $vv) {
                        $val['goodscount'] = $vv;
                    }
                    foreach ($userfans as $fan => $c) {
                        $val['fanscount'] = $c;
                    }
                }
                $friendsinfo[] = $val;
            }
        } else {
            $friendsinfo = 0;
        }
        return view('index/fans', [
            'friendsinfo' => $friendsinfo,
            'facecount' => $facecount,
            'friendscount' => $friendscount,
            'userinfo' => $userinfo,
            'page' => $page
        ]);

    }

    /**
     * 好友推荐列表
     * @return \think\response\View
     */
    public function friends()
    {
        $id = Request::instance()->session('uid');
        //获取用户的关注列表
        $res = Myredis::smembers($id . '_friends');
        //获取当前用户的基本信息
        $userinfo = Db::table('xo_user')->field(['icon', 'nickname'])->find($id);
        //获取当前用户的粉丝数量
        $facecount = Db::table('xo_user_friends')->where(['fid' => $id])->field(['uid'])->count();
        //获取当前用户的关注数量
        $friendscount = Db::table('xo_user_friends')->where(['uid' => $id])->field('fid')->count();
        if ($res) {
            foreach ($res as $key => $val) {
                //判断自己关注的用户和自己是不是互粉状态
                if (Myredis::sismber($val . '_friends', $id)) {
                    //获取互粉好友的好友列表
                    $fs = Myredis::smembers($val . '_friends');
                    foreach ($fs as $k => $v) {
                        //判断是否有共同好友,没有则保存
                        if (!MyRedis::sismber($id . '_friends', $v) && $v != $id) {
                            $friends[] = $v;
                        }
                    }
                }
            }
            if(!empty($firends)) {
                $gromm = array_unique($friends);
                foreach ($gromm as $key => $val) {
                    $friendsinfo[] = Db::table('xo_user')->field(['id', 'nickname', 'icon'])->find($val);
                }
            }else{
                $friendsinfo = 0;
            }
        } else {
            $friendsinfo = 0;
        }
        return view('index/friends', [
            'friendsinfo' => $friendsinfo,
            'userinfo' => $userinfo,
            'fanscount' => $facecount,
            'friendscount' => $friendscount
        ]);
    }

    /**
     * 用户收藏列表
     * @return \think\response\View
     */
    public function collect()
    {
        //获取登录用户的id
        $id = Request::instance()->session('uid');
        //获取当前登录用户的基本信息
        $userinfo = Db::table('xo_user')->field(['icon', 'nickname'])->find($id);
        //获取当前用户的粉丝数量
        $facecount = Db::table('xo_user_friends')->where(['fid' => $id])->field(['uid'])->count();
        //获取当前用户的关注数量
        $friendscount = Db::table('xo_user_friends')->where(['uid' => $id])->field('fid')->count();
        $res = Db::table('xo_user_goods')->where(['uid' => $id])->paginate(10);
        $page = $res->render();
        foreach ($res as $key => $val) {
            $res = Db::table('xo_goods_up')->where(['id' => $val['gid']])->field(['icon', 'id', 'desc', 'sold', 'house'])->select();
            if (!empty($res)) {
                $collect[] = $res;
            }
        }
        if (empty($collect)) {
            $collect = 0;
        }
        return view('index/collect', [
            'collect' => $collect,
            'friendscount' => $friendscount,
            'fanscount' => $facecount,
            'userinfo' => $userinfo,
            'page' => $page
        ]);
    }

    /**执行取消关注
     * @param Request $request
     * @return \think\response\Json|void
     */
    public function cancel(Request $request)
    {
        if (!Request::instance()->isAjax()) {
            return $this->redirect('index/error/index');
        }
        $uid = Session::get('uid');
        $gid = Request::instance()->get('gid');
        $res = Db::table('xo_user_goods')->where([
            'uid' => $uid,
            'gid' => $gid
        ])->delete();
        if ($res > 0) {
            $data = [
                'status' => true,
                'info' => '操作成功'
            ];
        } else {
            $data = [
                'status' => false,
                'info' => '操作失败'
            ];
        }
        return json($data);
    }

    /**
     * 执行收藏列表的收藏和取消收藏
     * @return \think\response\Json|void
     */
    public function docollect()
    {
        if (!Request::instance()->isAjax()) {
            return $this->redirect('index/error/index');
        }
        $uid = Request::instance()->session('uid');
        $gid = Request::instance()->get('gid');
        $ures = Db::table('xo_user_goods')->where(['uid'=>$uid,'gid'=>$gid])->find();
        if(!empty($ures)){
            $res = Db::table('xo_user_goods')->where(['uid'=>$uid,'gid'=>$gid])->delete();
            if(!empty($res)){
                $data=[
                    'status'=>true,
                    'info'=>'取消收藏成功'
                ];
            }
        }else{
            $res =Db::table('xo_user_goods')->insert(['uid'=>$uid,'gid'=>$gid]);
            if(!empty($res)){
                $data=[
                    'status'=>false,
                    'info'=>'收藏成功'
                ];
            }
        }
        return json($data);
    }
}
